package com.luceneheritrixbook.extractor.com163;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.util.NodeList;

import com.luceneheritrixbook.extractor.Extractor;
import com.luceneheritrixbook.util.StringUtils;

public class Extract163Moblie extends Extractor {

	private static final String MATCH_STRING1 = "<td width=\"31%\" align=\"left\" bgcolor=\"#F1F1F1\" class=\"fB\">(.*)</td>";

	public void extract() {
		BufferedWriter bw = null;
		String image_url = null;
		NodeFilter attributes_filter = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "parameter"));
		
		NodeFilter title_filter = new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class", "colA"));
		
		NodeFilter image_filter = new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class", "bigPic"));
		try {
			NodeList title_nodes = this.getParser().parse(title_filter);
			for (int i = 0; i < title_nodes.size(); i++) {
				//TableColumn title_node = (TableColumn) title_nodes.elementAt(i);
				
				//String[] names = title_node.getChildrenHTML().split(" ");
				
				String[] names = getProp("<h3>(.*)</h3>",title_nodes.elementAt(i).toHtml(),1).split(" "); 
				
				StringBuffer title = new StringBuffer();
				for (int k = 0; k < names.length; k++) {
					title.append(names[k]).append("-");
				}
				title.append((new Date()).getTime());
				String title_str = title.toString().replaceAll("/", "_");
				bw = new BufferedWriter(new FileWriter(new File(this
						.getOutputPath()
						+ title_str + ".txt")));
				int startPos = getInuputFilePath().indexOf("mirror") + 6;
				String url_seg = getInuputFilePath().substring(startPos);
				url_seg = url_seg.replaceAll("\\\\", "/");
				String url = "http:/" + url_seg;
				bw.write(url + NEWLINE);
				
				for (int k = 0; k < names.length; k++) {
					bw.write(names[k] + NEWLINE);
				}				
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		this.getParser().reset();
		try {
			NodeList attributes_nodes = this.getParser().parse(
					attributes_filter);
			for (int i = 0; i < attributes_nodes.size(); i++) {

				//TableColumn node = (TableColumn) attributes_nodes.elementAt(i);
				//String text = attributes_nodes.elementAt(i).getChildrenHTML();
				//if (node.getAttribute("width") != null
				//		&& node.getAttribute("width").equals("31%")) {
				
				NodeList  childnodes =  attributes_nodes.elementAt(i).getChildren();
				for (int j = 0; j < childnodes.size(); j++) {
					String text = childnodes.elementAt(j).toHtml();
					String tag = childnodes.elementAt(j).getText();
					
					if( tag.equals(new String("script")))
					{
						//String result = getProp("<script>\"(.*)\"</script>\"};", text, 0);
						String result = text.substring(text.indexOf("var json = {") + 12 , text.indexOf("</script>") );
						
						if( result != null )
						{
							result.replaceAll("\"", " ");
							String[] params = result.split(",");
							for(int k =0; k< params.length; k++)
							{ 
								bw.write(StringUtils.trim(params[k]) + NEWLINE);
							}
							break;
						}
					}
				}
				
				//	TableColumn nodeExt = (TableColumn) node.getNextSibling().getNextSibling();
					//bw.write(StringUtils.trim(result) + ":"	+ StringUtils.trim(nodeExt.getChildrenHTML())+ NEWLINE);
					
				//}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.getParser().reset();
		try {
			NodeList image_nodes = this.getParser().parse(image_filter);
			for (int i = 0; i < image_nodes.size(); i++) {
				//TableColumn image_node = (TableColumn) image_nodes.elementAt(i);
				
				image_url = getProp("<img src=\"(.*)\" onerror=\"(.*)\" />",image_nodes.elementAt(i).toHtml(),1);
				//image_url = getProp("<img src=\"(.*)\"  />", image_node.toHtml(), 1);
				
				if( image_url != null)
				{
					String fileType = image_url.substring(image_url.lastIndexOf(".") + 1);
					String new_image_file = StringUtils.encodePassword(image_url,HASH_ALGORITHM)
													+ "." + fileType;
					copyImage(image_url, new_image_file);
					bw.write(image_url + NEWLINE);					
					bw.write(SEPARATOR + NEWLINE);
					bw.write(new_image_file + NEWLINE);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (bw != null)
				bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}
	
	public static void main(String[] agrs) throws Exception {

		Extract163Moblie ex = new Extract163Moblie();
		ex.setOutputPath("D:\\product\\tom\\");
	//	traverse(
	//			ex,
	//			new File(
	//					"D:/data/163_mobile/mirror/product.tech.163.com/mobile/product/Amoi/00000cOG.html"));
		
	    ex.loadFile("D:/data/163_mobile/mirror/product.tech.163.com/mobile/product/Amoi/00000cOG.html");
		ex.extract();	
		
	}
}
